package com.example.springbootorderrabbitmqconsoumer.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class TTLRabbitmqConfig {

    //声明 direct交换机
    @Bean
    public DirectExchange ttlExchange() {
        return new DirectExchange("ttl-direct-exchange", true, false);
    }

    //声明 队列 sms.direct.queue email.direct.queue duanxin.direct.queue
    @Bean
    public Queue ttlSmsQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-message-ttl", 5000);
        return new Queue("ttl.direct.queue", true, false, false, args);
    }

    @Bean
    public Queue ttlMessageQueue() {
        return new Queue("ttl.message.direct.queue", true);
    }

    //完成 交换机和队列的绑定
    @Bean
    public Binding ttlSmsBinding() {
        return BindingBuilder.bind(ttlSmsQueue()).to(ttlExchange()).with("ttl");
    }

    @Bean
    public Binding ttlMessageBinding() {
        return BindingBuilder.bind(ttlMessageQueue()).to(ttlExchange()).with("message");
    }
}
